package cn.iocoder.yudao.module.system.controller.admin;

import cn.iocoder.yudao.framework.common.exception.ServerException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "广西政务")
@RestController
@RequestMapping("/gx")
public class GxController {

    @PostMapping ("/tpInfo")
    @Operation(summary = "获取脱贫信息")
    public CommonResult<List<GxTpNumberVO>> tpInfo(@RequestBody List<String> ids) throws Exception{
        List<GxTpNumberVO> result = new ArrayList<>();
        for(String id :ids){
            GxTpNumberVO numberVO = new GxTpNumberVO();
            numberVO = getTpInfo(id);
            if(null != numberVO) {
                numberVO.setIdCard(id);
                result.add(numberVO);
            }
        }

        return success(result);
    }

    private GxTpNumberVO getTpInfo(String id) {
        try{
            GxTpNumberVO result = new GxTpNumberVO();
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建请求对象
            HttpGet httpGet = new HttpGet("http://fastcurd.gxxw.cn/data/index/xczx_jdlk?idcard="+id);
            //发送请求，接受响应结果
            CloseableHttpResponse response = httpClient.execute(httpGet);
            //获取服务端返回的状态码
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("服务端返回的状态码为：" + statusCode);

            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity);
            System.out.println("服务端返回的数据为：" + body);
            Document document = Jsoup.parse(body);
            Elements elementsByTag = document.getElementsByTag("tbody").get(0).getElementsByTag("td");

            result.setNumberCode(elementsByTag.get(0).text());
            result.setShi(elementsByTag.get(1).text());
            result.setXian(elementsByTag.get(2).text());
            result.setXiangZheng(elementsByTag.get(3).text());
            result.setCun(elementsByTag.get(4).text());
            result.setName(elementsByTag.get(5).text());
            result.setRelation(elementsByTag.get(6).text());
            result.setCount(elementsByTag.get(7).text());
            result.setYear(elementsByTag.get(8).text());

            //关闭资源
            response.close();
            httpClient.close();

            return result;
        }catch (Exception e){
           return null;
        }

    }
}
